﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Administration_Detail : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {
        using (var dc = new SwitchboardDataContext()) {
            if (!dc.Hosts.Any(x => x.UserName == User.Identity.Name.ToLower())) {
                this.AutoModeFormView1.Visible = false;
                this.LiteralNoHosts.Visible = true;
            }
        }
    }

    protected void RedirectTypeDropDownList_SelectedIndexChanged(object sender, EventArgs e) {
        var redirectTypeDropDownList = this.AutoModeFormView1.FindControl("RedirectTypeDropDownList") as DropDownList;
        var redirectLocationTextBox = this.AutoModeFormView1.FindControl("RedirectLocationTextBox") as TextBox;
        var requiredFieldValidator2 = this.AutoModeFormView1.FindControl("RequiredFieldValidator2") as RequiredFieldValidator;
        var regularExpressionValidator2 = this.AutoModeFormView1.FindControl("RegularExpressionValidator2") as RegularExpressionValidator;
        using (var dc = new SwitchboardDataContext()) {
            var rt = dc.RedirectTypes.SingleOrDefault(x => x.RedirectTypeId == int.Parse(redirectTypeDropDownList.SelectedValue));
            redirectLocationTextBox.Visible = rt.UseLocationHeader;
            requiredFieldValidator2.Enabled = rt.UseLocationHeader;
            regularExpressionValidator2.Enabled = rt.UseLocationHeader;
        }
    }

    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args) {
        var hostNameDropDownList = this.AutoModeFormView1.FindControl("HostNameDropDownList") as DropDownList;
        var localPathTextBox = this.AutoModeFormView1.FindControl("LocalPathTextBox") as TextBox;

        args.IsValid = !Entry.Exists(int.Parse(hostNameDropDownList.SelectedValue), localPathTextBox.Text);
    }

}
